// pages/whole-detail/whole-detail.js
const util = require('../../utils/util.js')
const auth = require('../../utils/auth.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    collectionStatus: true,
    pageId:'',
    couponImg:{
      couponTrue:'../../img/holiday-true.png',
      couponFalse:'../../img/holiday-false.png'
    },
    headImg:'',
    img360:'../../images/360.png',
    plane:'../../images/plane.png',
    titleCN:'',
    titleEN:'',
    loading:false,
    mainTemp:'',
    location:'Pohjoisesplanadi,Helsinki 00130,Finland',
    locationIcon:'../../images/location.png',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var that = this ;
      var id = options.id;
      that.setData({
         pageId:id
      });
      util.ajax({
        url:'act/actapi/detail',
        data: {
          id:that.data.pageId
      },
      header: {
          'content-type': 'application/json' // 默认值
      },
      success: function(res) {
          if(res.data){
            var collStatus = res.data.is_collect;
            var res = res.data;
            var img = res.head_url;
            var main = res.content;
            var mainTemp = [];
            var authorTemp = ''; //作者
            authorTemp = main.author.split(",");
            var contentTemp = '';
            contentTemp = main.content.split("<&p&>");
            for (var i = 0; i < contentTemp.length; i++) {
              mainTemp[i] = new Object();
              mainTemp[i].author = authorTemp[i];
              mainTemp[i].content = contentTemp[i];
              mainTemp[i].pic = main.pic_url[i];
            }
            console.log(mainTemp);
            var detail = res.brief;
            var titleCN = res.title;
            var titleEN = res.en_title;
            that.setData({
               main:main,
               mainTemp:mainTemp,
               detail:detail,
               titleCN:titleCN,
               titleEN:titleEN,
               headImg:img,
               loading:true
            });
            if(collStatus){
              that.setData({
                collectionStatus:false
              });
            }else{
              that.setData({
                 collectionStatus:true
              }); 
            }
          }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  turn: function (e) {
    wx.showToast({
      title: '已收藏',
      duration: 1000
    })
    this.setData({
      collectionStatus:false
    })
    var that=this;
     util.ajax({
      url: 'act/actapi/collect',
      data: {
          id:that.data.pageId,
          collect:1
      },
      header: {
          'content-type': 'application/json' // 默认值
      },
      success: function(res) {
          
       
      }
    })
  },
  back :function (e){
    wx.showToast({
      title: '已取消收藏',
      duration: 1000
    })
    this.setData({
      collectionStatus: true
    })
    var that=this;
     util.ajax({
      url: 'act/actapi/collect',
      data: {
          id:that.data.pageId,
          collect:2
      },
      header: {
          'content-type': 'application/json' // 默认值
      },
      success: function(res) {

      }
    })
  }
})